/**
 * 
 */
package net.sahv.bdyz.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

/**
 * @author 张天敏
 * @date 日期：2016年8月27日 时间：下午3:20:02
 */
public class LoadCfgUtil {
	
	public static Map<String, String> loadCfg() throws FileNotFoundException, IOException
	{
		Properties p = new Properties();
		/*InputStream in =getClass().getResourceAsStream("classpath:path.properties");*/
		//p.load(new FileReader(new File("path.properties")));
		p.load(LoadCfgUtil.class.getResource("/path.properties").openStream());
		
		Map<String,String> map=new HashMap<>();
		//获取目标主机的IP地址
		String hostString=p.getProperty("targetUrl.host");
		//获取目标主机的端口号
		String portString=p.getProperty("targetUrl.port");
		//获取目标主机上运行的项目名
		String projectString=p.getProperty("targetUrl.project");
		
		Set<Entry<Object, Object>> mapSet=p.entrySet();
		Iterator<Entry<Object, Object>> mapSetIterator=mapSet.iterator();
		while(mapSetIterator.hasNext()){
			Entry<Object, Object> mapSetIteratorOne=mapSetIterator.next();
			String keyString=(String) mapSetIteratorOne.getKey();
			String valueString=(String) mapSetIteratorOne.getValue();
			String[] keyStringArray=keyString.split("\\.");
			if(!keyStringArray[0].equals("")){
				if(keyStringArray[0].equals("targetUrl")){
					//如果是目标主机地址以及端口号，项目名，则不添加到list容器中
					if(keyString.equals(hostString)||keyString.equals(portString)||keyString.equals(projectString)){
						
					}else{
						String targetUrl=hostString+":"+portString+"/"+projectString+"/"+valueString;
						map.put(keyString, targetUrl);
					}
				}else{
					map.put(keyString, valueString);
				}
			}else{
				
			}
			
		}
		
		return map;
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException{
		/*System.out.println(LoadCfgUtil.class.getResource("/path.properties").toExternalForm()); 
		System.out.println(Thread.currentThread().getContextClassLoader().getResource("/path.properties"));
		TestMain.class.getResource("/config.properties").openStream();*/
		Properties p = new Properties();
		/*InputStream in =getClass().getResourceAsStream("classpath:path.properties");*/
		//p.load(new FileReader(new File("path.properties")));
		p.load(LoadCfgUtil.class.getResource("/path.properties").openStream());
		Map<String,String> map=new HashMap<>();
		//Properties p=loadCfg();
		//获取目标主机的IP地址
		String hostString=p.getProperty("targetUrl.host");
		//获取目标主机的端口号
		String portString=p.getProperty("targetUrl.port");
		//获取目标主机上运行的项目名
		String projectString=p.getProperty("targetUrl.project");
		
		Set<Entry<Object, Object>> mapSet=p.entrySet();
		Iterator<Entry<Object, Object>> mapSetIterator=mapSet.iterator();
		while(mapSetIterator.hasNext()){
			Entry<Object, Object> mapSetIteratorOne=mapSetIterator.next();
			String keyString=(String) mapSetIteratorOne.getKey();
			String valueString=(String) mapSetIteratorOne.getValue();
			//如果是目标主机地址以及端口号，项目名，则不添加到list容器中
			if(keyString.equals(hostString)||keyString.equals(portString)||keyString.equals(projectString)){
				
			}else{
				String targetUrl=hostString+":"+portString+"/"+projectString+"/"+valueString;
				map.put(keyString, targetUrl);
			}
			
			/*System.out.println(keyString);
			System.out.println(valueString);*/
		}
		
		//String writeTxt=p.getProperty("targetUrl.writeTxt");
		String writeTxt=map.get("targetUrl.imgUpLoadTest");
		System.out.println(writeTxt);
	}

}
